#include "unp.h"

int debugStat = 0;
int loginStat = 0;
char user[MAXLINE] = { '\0' };

int clientCommand(int sockfd, char buf[MAXLINE]) {

	char message[MAXLINE] = { '\0' };
	char comm[MAXLINE] = { '\0' };
	char data[MAXLINE] = { '\0' };

	if (buf[0] == '.') {
		sscanf(buf, "%s %s", comm, data);
		if (debugStat && strcmp(comm, ".debug")) // debug on & comm is not debug
			fprintf(stderr, "> ");

		if (!strcmp(comm, ".user")) {	// 230 user logged
			if (!strcmp(data, "\0")) {
				fprintf(stderr, "Illegal command \r\n");
			} else {
				sprintf(message, "USER %s \r\n", data);
				if (debugStat)
					fprintf(stderr, "%s", message);
				loginStat = 1;
				strcpy(user, data);
				Writen(sockfd, message, strlen(message));
			}
		} else if (!strcmp(comm, ".list")) {	//231 users
			sprintf(message, "LIST \r\n");
			if (debugStat)
				fprintf(stderr, "%s", message);
			Writen(sockfd, message, strlen(message));
		} else if (!strcmp(comm, ".debug")) {	// N/A
			if (debugStat == 0) {
				debugStat = 1;
				fprintf(stderr, "debug = TRUE\n");
			} else if (debugStat == 1) {
				debugStat = 0;
				fprintf(stderr, "debug = FALSE\n");
			}
		} else if (!strcmp(comm, ".quit")) {	//221 service closing
			sprintf(message, "QUIT \r\n");
			if (debugStat)
				fprintf(stderr, "%s", message);
			Writen(sockfd, message, strlen(message));
		} else if (!strcmp(comm, ".bingo")) {	//799 bingo
			if (loginStat == 1) {
				snprintf(message, MAXLINE, "BINGO %s: %s \r\n", user, data);
				if (debugStat)
					fprintf(stderr, "%s", message);
				Writen(sockfd, message, strlen(message));
			} else if (loginStat == 0) {
				snprintf(message, MAXLINE, "BINGO %s: %s \r\n", '\0',
						data);
				if (debugStat)
					fprintf(stderr, "BINGO attack None: %s \r\n", data);
				Writen(sockfd, message, strlen(message));
			}
		} else if (!strcmp(comm, ".mix")) {
			sprintf(message, "MIX \r\n");
			if (debugStat)
				fprintf(stderr, "%s", message);
			Writen(sockfd, message, strlen(message));
		} else {
			fprintf(stderr, "Illegal command\r\n");
		} // 500 Syntax error
	} else {
		if (loginStat == 1) {			// 299 from
			snprintf(message, MAXLINE, "%s %s: %s \r\n", "FROM", user, buf);
			if (debugStat)
				fprintf(stderr, "> %s", message);
			Writen(sockfd, message, strlen(message));
		} else if (loginStat == 0) {
			snprintf(message, MAXLINE, "FROM %s: %s \r\n", '\0', buf);
			if (debugStat)
				fprintf(stderr, "FROM None: %s \r\n", buf);
			Writen(sockfd, message, strlen(message));
		}
	}

	memset(buf, 0x00, strlen(buf));
	memset(message, 0x00, strlen(message));

	return debugStat;

}

void receiveMsg(int debugFlag, char buf[MAXLINE]) {
	char protocol[MAXLINE] = { '\0' };
	char receiveMsg[MAXLINE] = { '\0' };
	char *msg;

	fflush(stdout);
	sscanf(buf, "%s, %s", protocol, receiveMsg);

	if (debugFlag) {	// debug on
		sleep(1);
		fprintf(stderr, "< ");
		Write(fileno(stdout), buf, strlen(buf)); // show all string
		if (!strcmp(protocol, "299") || !strcmp(protocol, "231")
				|| !strcmp(protocol, "799")) { // message, list
			msg = strtok(buf, " ");
			msg = strtok(NULL, " ");
			msg = strtok(NULL, "NULL");

			Write(fileno(stdout), msg, strlen(msg));
		}
	} else { // debug off
		if (!strcmp(protocol, "299")
				|| !strcmp(protocol, "231" || !strcmp(protocol, "799"))) { // message, list
			msg = strtok(buf, " ");
			msg = strtok(NULL, " ");
			msg = strtok(NULL, "NULL");

			Write(fileno(stdout), msg, strlen(msg));
		} else if (!strcmp(protocol, "530") || !strcmp(protocol, "720")) {
			fprintf(stderr, "< ");
			Write(fileno(stdout), buf, strlen(buf));
		} else {
			fprintf(stderr, "< ");
			Write(fileno(stdout), buf, strlen(buf));
		}
	}

	fflush(stdout);
	memset(buf, 0x00, strlen(buf));
}
